<?php
	require_once('simpletest/autorun.php');
	require_once('../PHP/generic.php');
	
	class TestGenericParser extends UnitTestCase{
		
		function testTCScrape() {
			$parser = new GenericParser("TechCrunch");
			$parser->setUrl("http://techcrunch.com");
			$parser->base_path = "//div[@class='left-container']//div[starts-with(@id, 'post-')]";
			$parser->content_path = "h2[@class='headline']/a";
			$parser->content_attr = "";
			$parser->image_path = "div[starts-with(@class, 'media-container')]/a/img";
			$parser->image_attr = "data-src";
			$parser->url_path = "h2[@class='headline']/a";
			$parser->url_attr = "href";
			$posts = $parser->scrapePosts();
			
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, "http://techcrunch.com");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$html = curl_exec($ch);      
			curl_close($ch);
			
			//set up domxpath to query html
			$dom = new domDocument;
			@$dom->loadHTML($html);
			$dom->preserveWhiteSpace = true;
			$finder = new DomXPath($dom);
			//$classname = " thing id-t3";
			
			//Use query to find specific div with content
			$table = $finder->query("//div[@class='left-container']//div[starts-with(@id, 'post-')]/h2[@class='headline']/a");
			$test_content = $table->item(0)->nodeValue;
			$this->assertTrue($posts[0]->content == $test_content);
			$table = $finder->query("//div[@class='left-container']//div[starts-with(@id, 'post-')]/div[starts-with(@class, 'media-container')]/a/img");
			$test_image = $table->item(0)->getAttribute("data-src");
			$this->assertTrue($posts[0]->image == $test_image);
			$table = $finder->query("//div[@class='left-container']//div[starts-with(@id, 'post-')]/h2[@class='headline']/a");
			$test_url = $table->item(0)->getAttribute("href");
			$this->assertTrue($posts[0]->url == $test_url);
			
		}
	}